package com.fingard.dsp.bank.helpers;

import java.sql.Connection;
import java.sql.SQLException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import com.fingard.FGBiz;
import com.fingard.constant.Format;
import com.fingard.db.JDBCHelper;
import com.fingard.dsp.bank.helpers.dbhelpers.InsertHelper;
import com.fingard.dsp.bank.helpers.dbhelpers.LoadHelper;

/**批次操作
 * 
 * ！！注意：需在finally里显示调用close()方法*/
public class SeqBatchInfoHelper {
	
	private Connection partDbconn = null;
	
	/**！！注意：需在finally里显示调用close()方法
	 * @param 租户
	 * @param 日期*/
	public SeqBatchInfoHelper(String pTenant, Date pPartDate) throws Exception{
		partDbconn = FGBiz.partTable.getLoaclDbConnByPart(pTenant, pPartDate);
	}
	
	public void close(){
		if(partDbconn != null){
			try{
				partDbconn.close();
			}catch(Exception ex){
				FGBiz.limitMsg.exception("SeqBatchInfoHelper.closeLocalDbConn", ex);
			}
		}
	}
	
	/**按渠道分表，对应PartitionTable.initTableSeqBatchInfo*/
	public static String getTableName(String pBankCode){
		return "SEQBAT" + pBankCode.toUpperCase();
	}
	
	/**将批次信息写入数据库表*/
	public boolean insertSeqBatchInfo(SeqBatchInfo pBatInfo) throws Exception{
		String tabName = getTableName(pBatInfo.bankCode);
		pBatInfo.createdOn = new Date();
		pBatInfo.lastModifiedOn = new Date();
		pBatInfo.rowVersion = 1;
		int rowAff = InsertHelper.insertObject(partDbconn, tabName, pBatInfo);
		if(rowAff == 1){
			return true;
		}else{
			throw new Exception("写入"+tabName+"的影响行数："+rowAff);
		}
	}
	
	/**更新文件上传状态，更新成功后pBatInfo.rowVersion自动加1
	 * @throws Exception */
	public int updateUpFileState(SeqBatchInfo pBatInfo) throws Exception{
		String tabName = getTableName(pBatInfo.bankCode);
		String upSql = "update "+tabName+" set UPFILESTATE=?,LASTMODIFIEDON=?,ROWVERSION=ROWVERSION+1 where REQBATCHNO=? and ROWVERSION=?";
		Object[] objs = new Object[4];
		objs[0] = pBatInfo.upFileState;
		objs[1] = new SimpleDateFormat(Format.DateIsoFormatStr).format(new Date());
		objs[2] = pBatInfo.reqBatchNo;
		objs[3] = pBatInfo.rowVersion;
		int rowAff = JDBCHelper.update(partDbconn, upSql, objs);
		if(rowAff == 1){
			pBatInfo.rowVersion = pBatInfo.rowVersion+1;
		}
		return rowAff;
	}
	
	public int updateDownFileStateAndPath(SeqBatchInfo pBatInfo) throws Exception{
		String tabName = getTableName(pBatInfo.bankCode);
		String upSql = "update "+tabName+" set DOWNFILESTATE=?,DOWNFILEPATH=?,LASTMODIFIEDON=?,ROWVERSION=ROWVERSION+1 where REQBATCHNO=? and ROWVERSION=?";
		Object[] objs = new Object[5];
		objs[0] = pBatInfo.downFileState;
		objs[1] = pBatInfo.downFilePath;
		objs[2] = new SimpleDateFormat(Format.DateIsoFormatStr).format(new Date());
		objs[3] = pBatInfo.reqBatchNo;
		objs[4] = pBatInfo.rowVersion;
		int rowAff = JDBCHelper.update(partDbconn, upSql, objs);
		if(rowAff == 1){
			pBatInfo.rowVersion = pBatInfo.rowVersion+1;
		}
		return rowAff;
	}
	
	/**按顺序获取当天下一个批次序号，条件：根据前置机代码+日期，
	 * 如果当天还没有批次产生，则从1开始编，即如果返回1，当天还没有批次，本次是第一批
	 * @throws SQLException */
	public int getNextBatSeq(SeqBatchInfo pBatInfo) throws SQLException{
		return getNextBatSeq(pBatInfo, 0);
	}
	
	/**按顺序获取当天下一个批次序号，条件：根据前置机代码+日期，
	 * 如果当天还没有批次产生，则从1开始编，即如果返回1，当天还没有批次，本次是第一批
	 * @param 文件顺序号基点，以此加1开始编
	 * @throws SQLException */
	public int getNextBatSeq(SeqBatchInfo pBatInfo, int baseSeq) throws SQLException{
		String tabName = getTableName(pBatInfo.bankCode);
		String sql = "select max(BATSEQNO) from "+tabName+" where PAYDATE=? and BANKFRONTCODE=?";
		Object[] objs = new Object[2];
		objs[0] = pBatInfo.payDate;
		objs[1] = pBatInfo.bankFrontCode;
		Object maxNo = JDBCHelper.execScalar(partDbconn, sql, objs);
		if(maxNo!=null && maxNo.toString().length() > 0){
			return Integer.parseInt(maxNo.toString())+1;
		}
		if(baseSeq>0){
			return baseSeq+1;
		}else{
			return 1;
		}
	}
	
	public SeqBatchInfo loadFromDb(String pBankCode, String pReqBatchNo) throws SQLException, IllegalArgumentException, IllegalAccessException, ParseException{
		String tabName = getTableName(pBankCode);
		String sql = "select * from "+tabName+" where REQBATCHNO=?";
		Object[] objs = new Object[1];
		objs[0] = pReqBatchNo;
		Map<String,Object> mapData = JDBCHelper.execQuerySingle(partDbconn, sql, objs);
		if(mapData != null && mapData.size() > 0){
			SeqBatchInfo batInfo = new SeqBatchInfo();
			LoadHelper.loadObject(mapData, batInfo);
			return batInfo;
		}
		return null;
	}
}
